//Khai bao thu vien
#include<stdio.h>
#include<conio.h>
#include <math.h>
//Dinh nghia cac kieu du lieu cau truc
//DIEM:kieu du lieu cau truc gom 2 truong x,y
typedef struct
{
    float x;
    float y;
}DIEM;
//CACDIEM:kieu du lieu cau truc gom 3 truong A,B,C
typedef struct
{
    DIEM A;
    DIEM B;
    DIEM C;
}CACDIEM;
//Ham nhap 1 diem
void Nhapdiem(DIEM &d)
{
    printf("\n\tNhap hoanh do x:");
    scanf("%f",&d.x);
    printf("\n\tNhap tung do y:");
    scanf("%f",&d.y);
}
//Ham nhap 3 diem A,B,C
void Nhapcacdiem(CACDIEM &cd)
{
    printf("\n\tNhap toa do DIEM A:\n");
    Nhapdiem(cd.A);							//Goi ham nhap 1 diem
    printf("\n\tNhap toa do DIEM B:\n");
    Nhapdiem(cd.B);							//Goi ham nhap 1 diem
    printf("\n\tNhap toa do diem C:\n");
    Nhapdiem(cd.C);							//Goi ham nhap 1 diem
}
//Ham kiem tra diem
int kiemtrad(CACDIEM cd)
{
	//Kiem tra toa do cac diem co trung nhau khong
	if((cd.A.x==cd.B.x&&cd.A.y==cd.B.y)||(cd.A.x==cd.C.x&&cd.A.y==cd.C.y)||
		(cd.B.x==cd.C.x&&cd.B.y==cd.C.y))	
		return 1;
	else	
		return 0;
}
//Ham tinh do dai cac canh AB,AC,BC
void dodai(CACDIEM cd,float &k,float &s,float &t)
{	
	k=sqrt(pow((cd.B.x-cd.A.x),2)+pow((cd.B.y-cd.A.y),2));
	s=sqrt(pow((cd.C.x-cd.A.x),2)+pow((cd.C.y-cd.A.y),2));
	t=sqrt(pow((cd.C.x-cd.B.x),2)+pow((cd.C.y-cd.B.y),2));
}
//Ham kiem tra xem ba canh AB,AC,BC co phai la 3 canh tam giac khong, neu la tam
//giac thi tinh dien tich
void kiemtratg(float k,float s,float t)
{
	float p,kq;
	if((k+s)>t&&(k+t)>s&&(s+t)>k)	//Kiem tra tong 2 canh so voi canh con lai
	{
		p=(k+s+t)/2;
		kq=sqrt(p*(p-k)*(p-t)*(p-s));
		printf("\n\tDiem tich tam giac ABC la:%f",kq);
	}
	else
		printf("\n\tKhong phai la tam giac");	
}
//Ham menu
void menu(int &chon)
{
	printf("\n\t1.Nhap toa do 3 diem  A,B,C phan biet");
	printf("\n\t2.Tinh do dai cac canh AB,BC,AC");
	printf("\n\t3.Kiem tra xem AB,BC,AC co phai la ba canh cua tam giac khong");
	printf("\n\tva tinh dien tich neu la tam giac");
	printf("\n\t0.Thoat");
	printf("\n\tChon cong viec:");
	scanf("%d",&chon);
}
void main()
{
	int kq,chon;
	float k,s,t;
 	CACDIEM cd;
	 do
	 {
		clrscr();
		//Goi ham menu
		menu(chon);
		switch(chon)
		{
			//Nhap diem, neu diem co toa do trung nhau thi nhap lai
			case 1:
				do
				{
					clrscr();
					Nhapcacdiem(cd);
		    		kq=kiemtrad(cd);
				}
				while(kq==1);
				break;
			//Tinh do dai
			case 2:
				dodai(cd,k,s,t);
				printf("\n\tDo dai cac canh la:%10.1f%10.1f%10.1f",k,s,t);
				break;
			//Kiem tra tam giac
			case 3:
				kiemtratg(k,s,t);
				break;
		}
		getch();
	 }
	 while(chon!=0);
}

